/**
  ******************************************************************************
  * @file    YDDO0196AAFD.c
  * @author  zhaitao
  * @brief   lcd: YDDO0196AAFD driver.
  * 
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 YES-LCD.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by YES-LCD under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
#include "YDDO0196AAFD.h"

const uint8_t ICON_A_INDEX[] = 
{
    3,
    21,
    53,
    4,
    19,
    20,
    37,
    29,
    52,
    68,
    83,
    84,
    100,
    92,
};

const uint8_t ICON_B_INDEX[] =
{
    101,
    102,
    103,
    104,
    105,
    106,
};

/**
  * @brief o0196 draw icon
  * @param iconGroup ICON_GROUP_A or ICON_GROUP_B
  * @param iconIndex index in That group
  * @param state display state, 0=off, 1=on
  * @retval None
  */
void o0196_drawIcon(uint8_t iconGroup, uint8_t iconIndex, uint8_t state)
{
    uint8_t index = 0;
    switch (iconGroup)
    {
    case ICON_GROUP_A:
        index = ICON_A_INDEX[iconIndex - 1];
        break;
    case ICON_GROUP_B:
        index = ICON_B_INDEX[iconIndex - 1];
        break;
    default:
        break;
    }

    st7567_drawIcon(index, state);
}

//   A   B   C   D   E   F   G
const uint8_t DIGIT_NUM_SEG_TABLE[12][7] = 
{
    {38, 39, 42, 44, 43, 40, 41},   //1
    {45, 46, 49, 51, 50, 47, 48},   //2
    {54, 55, 58, 60, 59, 56, 57},   //3
    {61, 62, 65, 67, 66, 63, 64},   //4
    {5,  6,  9,  11, 10, 7,  8 },   //5
    {12, 13, 16, 18, 17, 14, 15},   //6
    {22, 23, 26, 28, 27, 24, 25},   //7
    {30, 31, 34, 36, 35, 32, 33},   //8
    {69, 70, 73, 75, 74, 71, 72},   //9
    {76, 77, 80, 82, 81, 78, 79},   //10
    {85, 86, 89, 91, 90, 87, 88},   //11
    {93, 94, 97, 99, 98, 95, 96},   //12
};

//   A   B   C   D   E   F   G
const uint8_t DIGIT_NUM_PRINT_LOOKUP[10][7] = 
{
    {1,  1,  1,  1,  1,  1,  0}, //0
    {0,  1,  1,  0,  0,  0,  0}, //1
    {1,  1,  0,  1,  1,  0,  1}, //2
    {1,  1,  1,  1,  0,  0,  1}, //3
    {0,  1,  1,  0,  0,  1,  1}, //4
    {1,  0,  1,  1,  0,  1,  1}, //5
    {1,  0,  1,  1,  1,  1,  1}, //6
    {1,  1,  1,  0,  0,  0,  0}, //7
    {1,  1,  1,  1,  1,  1,  1}, //8
    {1,  1,  1,  1,  0,  1,  1}, //9
};

/**
  * @brief Draw 7seg digit number
  * @param index digit-number index(1-12)
  * @param num value(0-9)
  * @retval None
  */
void o0196_draw_digit(uint8_t index, uint8_t num)
{
    for(uint8_t seg = 0; seg < 7; seg++)
    {
        st7567_drawIcon(DIGIT_NUM_SEG_TABLE[index-1][seg], DIGIT_NUM_PRINT_LOOKUP[num][seg]);
    }
}

void o0196_digit_left(float num);
void o0196_digit_right(float num);
void o0196_digit_HH(float num);
void o0196_digit_MM(float num);

/**
  * @brief test exmaple
  * @param None
  * @retval None
  */
void o0196_tests(void)
{
    // res = 0;
	HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET);
	HAL_Delay(100);
	// res = 1;
	HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET);
	HAL_Delay(100);

	st7567s_initial();

    st7567_fillBuffer(0x00);
    st7567_flushBuffer();
    HAL_Delay(5);

    while(1){
        
    /*
        st7567_drawIcon(5, state);
        st7567_drawIcon(6, state);
        st7567_drawIcon(7, state);
        st7567_drawIcon(8, state);
        st7567_drawIcon(9, state);
        st7567_drawIcon(10, state);
        st7567_drawIcon(11, state);
        HAL_Delay(500);

        st7567_drawIcon(5, 0);
        st7567_drawIcon(6, 0);
        st7567_drawIcon(7, 0);
        st7567_drawIcon(8, 0);
        st7567_drawIcon(9, 0);
        st7567_drawIcon(10, 0);
        st7567_drawIcon(11, 0);
        HAL_Delay(500);
        HAL_Delay(500);
    */
        
    /*
        o0196_drawIcon(ICON_GROUP_A, 1, 1);
        HAL_Delay(500);
        
        o0196_drawIcon(ICON_GROUP_A, 1, 0);
        HAL_Delay(500);
    */

    /*        
        for(uint8_t i = 1; i <= 14; i++)
        {
            o0196_drawIcon(ICON_GROUP_A, i, 1);
            HAL_Delay(500);
            
            o0196_drawIcon(ICON_GROUP_A, i, 0);
            HAL_Delay(50);
        }

        for(uint8_t i = 1; i <= 6; i++)
        {
            o0196_drawIcon(ICON_GROUP_B, i, 1);
            HAL_Delay(500);
            
            o0196_drawIcon(ICON_GROUP_B, i, 0);
            HAL_Delay(50);
        }
    */
        
        for(uint8_t j = 0; j <=9; j++)
        {
            for(uint8_t i = 1; i <= 12; i++)
            {
                o0196_draw_digit(i, j);
            }
            HAL_Delay(500);
        }
        

    }
    
}